package com.hht.webpackagekit.inner;

import android.content.Context;
import android.util.Log;

import com.hht.webpackagekit.core.Constants;
import com.hht.webpackagekit.core.Downloader;
import com.hht.webpackagekit.core.PackageInfo;
import com.hht.webpackagekit.core.util.FileUtils;
import com.hht.webpackagekit.core.util.Logger;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadSampleListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.model.FileDownloadStatus;

/**
 * 下载器实现类
 */
public class DownloaderImpl implements Downloader {
    private final Context context;
    private String downloadUrl;

    public DownloaderImpl(Context context) {
        this.context = context;
    }

    //根据PackageInfo的downloadUrl下载离线包到PackageDownloadName: ${root}/${packageId}/${version}/download.zip
    @Override
    public void download(final PackageInfo packageInfo, final DownloadCallback callback) {
        downloadUrl = packageInfo.getDownloadUrl();
        Log.d("download", downloadUrl);

        BaseDownloadTask downloadTask = FileDownloader.getImpl()
                .create(downloadUrl)
                .addHeader("If-Match", "")
//                .setTag(packageInfo.getOrigin_file_md5())
                .setPath(FileUtils.getPackageDownloadName(context, packageInfo.getPackageId(), packageInfo.getVersion()))
                .setListener(new FileDownloadSampleListener() {
                    @Override
                    protected void completed(BaseDownloadTask task) {
                        super.completed(task);
                        if (callback != null && task.getStatus() == FileDownloadStatus.completed) {
                            callback.onSuccess(packageInfo.getOrigin_file_md5());
                        } else if (callback != null) {
                            callback.onFailure(packageInfo.getOrigin_file_md5());
                        }
                    }

                    @Override
                    protected void error(BaseDownloadTask task, Throwable e) {
                        super.error(task, e);
                        Logger.e("packageResource download error: " + e.getMessage());
                        if (callback != null) {
                            callback.onFailure(packageInfo.getOrigin_file_md5());
                        }
                    }
                });
        downloadTask.start();
    }
}
